package com.springboot.mybatis.example.service;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.springboot.mybatis.example.dao.UserDao;
import com.springboot.mybatis.example.domain.User;
import com.springboot.mybatis.example.utils.RedisUtil;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
//	@Autowired
//	private RedisTemplate redisTemplate;
	@Autowired
	private RedisUtil redisUtil;

	public User getUserById(int userId) {
		String key = "user_" + userId;
//		ValueOperations<String, User> operations = redisTemplate.opsForValue();
		boolean hasKey = redisUtil.hasKey(key);
		if (hasKey) {
			User user = (User) redisUtil.get(key);
			System.err.println("从缓存中获取数据:" + user.getUserName());
			System.err.println("-------------------------");
			return user;
		} else {
			User user = userDao.findUserById(userId);
			System.err.println("从数据库中获取数据:" + user.getUserName());
			System.err.println("---------写入缓存----------------");
			// 写入缓存
			redisUtil.set(key, user);
			return user;
		}

	}

	public boolean addUser(User record) {
		boolean result = false;
		try {
			userDao.insertSelective(record);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 
	 * 获取用户策略:先从缓存中获取用户，没有则读mysql数据，再将数据写入缓存
	 * 
	 */

	public User findUserById(int id) {

		String key = "user_" + id;

//		ValueOperations<String, User> operations = redisTemplate.opsForValue();

		// 判断redis中是否有键为key的缓存

		boolean hasKey = redisUtil.hasKey(key);

		if (hasKey) {

			User user = (User) redisUtil.get(key);

			System.out.println("从缓存中获取数据:" + user.getUserName());

			System.out.println("-----------------------------");

			return user;

		} else {

			User user = userDao.findUserById(id);

			System.out.println("查询数据库获取数据:" + user.getUserName());

			System.out.println("------------写入缓存---------------------");

			// 写入缓存

			redisUtil.sSet(key, user, 5, TimeUnit.HOURS);

			return user;

		}

	}

	// 删除用户策略:删除数据表中数据，然后删除缓存

	public Boolean deleteUserById(int id) {

		Boolean result = userDao.deleteUserById(id);

		String key = "user_" + id;

		if (result) {

			boolean hasKey = redisUtil.hasKey(key);

			if (hasKey) {

				redisUtil.delete(key);

				System.out.println("删除了缓存中的key:" + key);

			}

		}

		return result;

	}

	/**
	 * 
	 * 更新用户策略：先更新数据表，成功之后，删除原来的缓存，再更新缓存
	 * 
	 */

	public int updateUser(User user) {

//		ValueOperations<String, User> operations = redisTemplate.opsForValue();

		int result = userDao.updateUser(user);

		if (result != 0) {

			String key = "user_" + user.getId();

			boolean haskey = redisUtil.hasKey(key);

			if (haskey) {

				redisUtil.delete(key);

				System.out.println("删除缓存中的key-----------> " + key);

			}

			// 再将更新后的数据加入缓存

			User userNew = userDao.findUserById(user.getId());

			if (userNew != null) {

				redisUtil.sSet(key, userNew, 3, TimeUnit.HOURS);

			}

		}

		return result;

	}

	@Override
	public User insertUser(int userId) {
		return null;
	}
}